import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

class FlingingRotationButton extends StatefulWidget {
  const FlingingRotationButton({Key? key}) : super(key: key);

  @override
  State<FlingingRotationButton> createState() => _FlingingRotationButtonState();
}

class _FlingingRotationButtonState extends State<FlingingRotationButton>
    with SingleTickerProviderStateMixin {
  late final AnimationController _animation;

  @override
  void initState() {
    super.initState();
    _animation = AnimationController(
      vsync: this,
      duration: Duration(seconds: 3),
    );
  }

  @override
  void dispose() {
    _animation.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("按钮旋转动画"),
      ),
      body: SafeArea(
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              RotationTransition(
                turns: _animation,
                child: TextButton(
                  child: Text('Hello '),
                  onPressed: () async {
                    try {
                      _animation.reset();
                      await _animation.fling().orCancel;
                    } on TickerCallback {
                      print("TickerCallback");
                    }
                  },
                ),
              ),
              InteractiveViewer(
                minScale: 0.2,
                maxScale: 5.0,
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    Transform(
                      transform: Matrix4.identity().scaled(1.03, 1.03, 1.0),
                      child: Icon(
                        Icons.all_inclusive_sharp,
                        color: Colors.black54.withAlpha(20),
                        size: 256.0,
                      ),
                    ),
                    Icon(
                      Icons.all_inclusive_sharp,
                      color: Colors.black54.withAlpha(20),
                      size: 256.0,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
